package cn.hbu.zlfw.patent.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import cn.hbu.zlfw.admin.entity.Expert;
import cn.hbu.zlfw.admin.entity.ScienceExpert;
import cn.hbu.zlfw.common.base.dao.impl.BaseDaoImpl;
import cn.hbu.zlfw.common.util.PageModel;
import cn.hbu.zlfw.common.util.Utility;
import cn.hbu.zlfw.dic.entity.DicPatentState;
import cn.hbu.zlfw.patent.dao.PatentDao;
import cn.hbu.zlfw.patent.entity.Patent;

/**
 * 
 * @ClassName: PatentDaoImpl
 * @Description: 专利dao接口实现
 * @author 王鹏伟<1223443289@qq.com>
 * @date 2017年11月2日 下午6:44:42
 * @version V1.0
 */
@Repository("patentDao")
public class PatentDaoImpl extends BaseDaoImpl<Patent, String> implements PatentDao {
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.hbu.zlfw.patent.dao.PatentDao#listPatentByParamsToResearcher(cn.hbu.zlfw.
	 * common.util.PageModel, java.lang.Integer, java.lang.String)
	 */

	@Override
	public List<Patent> listPatentByParamsToResearcher(Integer patentState, String name, String researcherId) {
		Map<String, Object> params = Utility.getMapInstance();
		params.put("id", researcherId);
		StringBuffer sbf = new StringBuffer();
		sbf.append("from Patent p inner join fetch p.researcher r where r.id=:id ");
		if (name != null && name != "") {
			params.put("name", "%" + name + "%");
			sbf.append("and p.name like :name ");
		}
		if (patentState != null && patentState != 0) {
			params.put("patentState", patentState);
			sbf.append("and p.patentState=:patentState ");
		}
		sbf.append("order by p.updateTime desc");
		Query query = this.getSession().createQuery(sbf.toString());
		return Utility.queryParameterUtil(query, params).list();
	}

	/*
	 * 分页条件查询科技服务人员已接单专利的相关信息
	 */
	@Override
	public PageModel<Patent> listPatentByParamsToScience(PageModel<Patent> pageModel, String name, String scienceId) {
		Map<String, Object> params = Utility.getMapInstance();
		params.put("id", scienceId);
		params.put("patentState", DicPatentState.RECEIVED);
		StringBuffer sbfhql = new StringBuffer();
		StringBuffer sbfhqlCount = new StringBuffer();
		sbfhql.append(
				"from Patent p inner join fetch p.scienceExpert se where se.id=:id and p.patentState=:patentState ");
		sbfhqlCount.append(
				"select count(*) from Patent p inner join p.scienceExpert se where se.id=:id and p.patentState=:patentState ");
		if (name != null && name != "") {
			params.put("name", "%" + name + "%");
			sbfhql.append("and p.name like :name ");
			sbfhqlCount.append("and p.name like :name ");
		}
		return this.findListByParams(sbfhql.toString(), sbfhqlCount.toString(), params, pageModel);
	}

	/*
	 * 分页条件查询科技服务人员所在单位的全部专利和允许括单位协助专利:未接单专利
	 */
	public PageModel<Patent> listAllPatentByParamsToScience(PageModel<Patent> pageModel, String name,
			Integer schoolType) {
		StringBuffer sbfhql = new StringBuffer();
		StringBuffer sbfhqlCount = new StringBuffer();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("allowCorss", 1);
		params.put("schoolType", schoolType);
		params.put("patentState", DicPatentState.WAIT_RECEIVE);
		sbfhql.append("from Patent p inner join fetch p.researcher r "
				+ "where (p.allowCorss =:allowCorss or r.schoolType=:schoolType) and p.patentState=:patentState ");
		sbfhqlCount.append("select count(*) from Patent p inner join p.researcher r  "
				+ "where (p.allowCorss =:allowCorss or r.schoolType=:schoolType) and p.patentState=:patentState ");
		if (name != null && name != "") {
			params.put("name", "%" + name + "%");
			sbfhql.append(" and p.name like :name ");
			sbfhqlCount.append(" and p.name like :name ");
		}
		return this.findListByParams(sbfhql.toString(), sbfhqlCount.toString(), params, pageModel);
	}

	/*
	 * 分页条件查询科技服务人员协助专利列表:专利状态为已接单状态
	 */
	@Override
	public PageModel<Patent> listAssistPatentByParamsToScience(PageModel<Patent> pageModel, String name,
			String assistScienceId) {
		Map<String, Object> params = Utility.getMapInstance();
		params.put("id", assistScienceId);
		params.put("patentState", DicPatentState.RECEIVED);
		StringBuffer sbf = new StringBuffer();
		StringBuffer sbfhqlCount = new StringBuffer();
		sbf.append(
				"from Patent p inner join fetch p.scienceExperts ses where ses.id=:id and p.patentState=:patentState ");
		sbfhqlCount.append(
				"select count(*) from Patent p inner join p.scienceExperts ses where ses.id=:id and p.patentState=:patentState ");
		if (name != null && name != "") {
			params.put("name", "%" + name + "%");
			sbf.append("and p.name like :name ");
			sbfhqlCount.append("and p.name like :name ");
		}
		return this.findListByParams(sbf.toString(), sbfhqlCount.toString(), params, pageModel);
	}

	/*
	 * 分页条件查询科技服务人员所有结项专利列表
	 */
	@Override
	public PageModel<Patent> listFinishPatentByParamsToScience(PageModel<Patent> pageModel, String name,
			String scienceId) {
		Map<String, Object> params = Utility.getMapInstance();
		params.put("id", scienceId);
		params.put("patentState", DicPatentState.FINISHED);
		StringBuffer sbfhql = new StringBuffer();
		StringBuffer sbfhqlCount = new StringBuffer();
		sbfhql.append(
				"from Patent p inner join fetch p.scienceExpert se where se.id=:id and p.patentState=:patentState ");
		sbfhqlCount.append(
				"select count(*) from Patent p inner join p.scienceExpert se where se.id=:id and p.patentState=:patentState ");
		if (name != null && name != "") {
			params.put("name", "%" + name + "%");
			sbfhql.append("and p.name like :name ");
			sbfhqlCount.append("and p.name like :name ");
		}
		return this.findListByParams(sbfhql.toString(), sbfhqlCount.toString(), params, pageModel);
	}

	/*
	 * 分分页查询与科技服务人员学科相关的未接单专利
	 */
	@Override
	public PageModel<Patent> listPatentOfSubjectRelatedToScience(PageModel<Patent> pageModel, String name,
			ScienceExpert scienceExpert) {
		StringBuffer sbfhql = new StringBuffer();
		StringBuffer sbfhqlCount = new StringBuffer();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("allowCorss", 1);
		params.put("schoolType", scienceExpert.getSchoolType());
		params.put("patentState", DicPatentState.WAIT_RECEIVE);
		sbfhql.append("from Patent p inner join fetch p.researcher r "
				+ "where (p.allowCorss =:allowCorss or r.schoolType=:schoolType) and p.patentState=:patentState ");
		sbfhqlCount.append("select count(*) from Patent p inner join p.researcher r  "
				+ "where (p.allowCorss =:allowCorss or r.schoolType=:schoolType) and p.patentState=:patentState ");
		if (name != null && name != "") {
			params.put("name", "%" + name + "%");
			sbfhql.append("and p.name like :name ");
			sbfhqlCount.append("and p.name like :name ");
		}
		sbfhql.append("and ( ");
		sbfhqlCount.append("and ( ");
		String[] subjectTypeCodes = scienceExpert.getSubjectTypes().split(",");
		for (int i = 0; i < subjectTypeCodes.length; i++) {
			params.put("subjectTypeCode" + i, "%" + subjectTypeCodes[i] + "%");
			sbfhql.append("p.subjectTypes like :subjectTypeCode" + i + " or ");
			sbfhqlCount.append("p.subjectTypes like :subjectTypeCode" + i + " or ");
		}
		String sbfhqlString = sbfhql.substring(0, sbfhql.length() - 3) + " )";
		String sbfhqlCountString = sbfhqlCount.substring(0, sbfhqlCount.length() - 3) + " )";
		return this.findListByParams(sbfhqlString, sbfhqlCountString, params, pageModel);
	}

	/*
	 * 单位管理员查询所有专利:即本单位所有未接单专利和所有允许跨单位未接单专利
	 */
	@Override
	public PageModel<Patent> listAllPatentByParamsToUnit(PageModel<Patent> pageModel, String name, Integer schoolType) {
		return listAllPatentByParamsToScience(pageModel, name, schoolType);
	}

	/*
	 * 单位管理员分页查询所有本单位和允许跨单位已接单专利的相关信息
	 */
	@Override
	public PageModel<Patent> listPatentByParamsToUnit(PageModel<Patent> pageModel, String name, Integer schoolType) {
		StringBuffer sbfhql = new StringBuffer();
		StringBuffer sbfhqlCount = new StringBuffer();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("allowCorss", 1);
		params.put("schoolType", schoolType);
		params.put("patentState", DicPatentState.RECEIVED);
		sbfhql.append("from Patent p inner join fetch p.researcher r "
				+ "where (p.allowCorss =:allowCorss or r.schoolType=:schoolType) and p.patentState=:patentState ");
		sbfhqlCount.append("select count(*) from Patent p inner join p.researcher r  "
				+ "where (p.allowCorss =:allowCorss or r.schoolType=:schoolType) and p.patentState=:patentState ");
		if (name != null && name != "") {
			params.put("name", "%" + name + "%");
			sbfhql.append(" and p.name like :name ");
			sbfhqlCount.append(" and p.name like :name ");
		}
		return this.findListByParams(sbfhql.toString(), sbfhqlCount.toString(), params, pageModel);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.hbu.zlfw.patent.dao.PatentDao#listPatentOfSubjectRelatedToExpert(cn.hbu.
	 * zlfw.common.util.PageModel, java.lang.String,
	 * cn.hbu.zlfw.admin.entity.Expert)
	 */
	@Override
	public PageModel<Patent> listPatentOfSubjectRelatedToExpert(PageModel<Patent> pageModel, String name,
			Expert expert) {
		// TODO Auto-generated method stub
		StringBuffer sbfhql = new StringBuffer();
		StringBuffer sbfhqlCount = new StringBuffer();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("allowCorss", 1);
		params.put("schoolType", expert.getSchoolType());
		params.put("patentState", DicPatentState.WAIT_RECEIVE);
		sbfhql.append("from Patent p inner join fetch p.researcher r "
				+ "where (p.allowCorss =:allowCorss or r.schoolType=:schoolType) and p.patentState=:patentState ");
		sbfhqlCount.append("select count(*) from Patent p inner join p.researcher r  "
				+ "where (p.allowCorss =:allowCorss or r.schoolType=:schoolType) and p.patentState=:patentState ");
		if (name != null && name != "") {
			params.put("name", "%" + name + "%");
			sbfhql.append("and p.name like :name ");
			sbfhqlCount.append("and p.name like :name ");
		}
		sbfhql.append("and ( ");
		sbfhqlCount.append("and ( ");
		String[] subjectTypeCodes = expert.getSubjectTypes().split(",");
		for (int i = 0; i < subjectTypeCodes.length; i++) {
			params.put("subjectTypeCode" + i, "%" + subjectTypeCodes[i] + "%");
			sbfhql.append("p.subjectTypes like :subjectTypeCode" + i + " or ");
			sbfhqlCount.append("p.subjectTypes like :subjectTypeCode" + i + " or ");
		}
		String sbfhqlString = sbfhql.substring(0, sbfhql.length() - 3) + " )";
		String sbfhqlCountString = sbfhqlCount.substring(0, sbfhqlCount.length() - 3) + " )";
		return this.findListByParams(sbfhqlString, sbfhqlCountString, params, pageModel);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.hbu.zlfw.patent.dao.PatentDao#lisAllfinishPatent()
	 */
	@Override
	public PageModel<Patent> lisAllfinishPatent(PageModel<Patent> pageModel,Integer state) {
		Map<String, Object> params = Utility.getMapInstance();
		StringBuffer sbfhql = new StringBuffer();
		StringBuffer sbfhqlCount = new StringBuffer();
		sbfhql.append("from Patent p where p.patentState = "+state);
		sbfhqlCount.append("select count(*) from Patent p where p.patentState = "+state);
		return this.findListByParams(sbfhql.toString(), sbfhqlCount.toString(), params, pageModel);
	}
}
